package com.common.frame.widget;

import android.content.Context;
import android.os.Handler;
import android.widget.Toast;

public class CustomToast {

    /** 用于测试 */

    private int showCount = 1;

    private Toast toast = null;

    private Context context;

    private Handler handler = null;

    private Runnable toastThread = new Runnable() {

        public void run() {

            // 递增的count明显地表明是不断运行新的Toast.show()的结果。

//            toast.setText(String.valueOf(showCount++) + "CustomToast");

            toast.show();

            // 3.3秒后再度重启，设为4s的话将会看到Toast是断断续续地显示着的。

            handler.postDelayed(toastThread, 3300);

        }

    };



    public CustomToast(Context context) {

        this.context = context;

        handler = new Handler(this.context.getMainLooper());

        toast = Toast.makeText(this.context, "", Toast.LENGTH_LONG);

    }



    public void setText(String text) {

        toast.setText(text);

    }



    public void showToast(final long length) {

        handler.post(toastThread);


        Thread timeThread = new Thread() {

            public void run() {


                try {

                    Thread.sleep(length);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

                CustomToast.this.stopToast();

            }

        };

        timeThread.start();

    }



    public void stopToast() {

        // 删除Handler队列中的仍处理等待的消息元素删除

        handler.removeCallbacks(toastThread);

        // 撤掉仍在显示的Toast

        toast.cancel();

    }

}
//复制代码然后调用： CustomToast customToast = new CustomToast(this);
//
//        customToast.setText("Sodino Show 5s");
//
//        customToast.showToast(5000l);

//要提前停止的话调用

//customToast.stopToast();